if (get_cookie("key")) {
    $.ajax({
        url: ip + "index.php?s=admincms/login/isadmin",
        type: "post",
        data: {
            "user": get_cookie("user"),
            "key": get_cookie("key")
        },
        success: function (_res) {
            res = JSON.parse(_res);
            if (res.resStatus === 0) {
                // 用户写入页面
                $("#user_name").text(res.data[0].NickName);
                $(".content_left>ul").empty();
                var url=location.href;
                var tiao=true;
                $.each(res.data[0].power_list,function (i,o) {
                    if(url.indexOf(o.url)!==-1){
                        tiao=false;
                    }
                    var _background_=  url.indexOf(o.url)!==-1?'style="background:#000;"':"";

                    $(".content_left>ul").append('<a href="'+o.url+'"> <li '+_background_+'><span class="glyphicon '+o.icon+'"></span><p>'+o.names+'</p></li></a>');

                });

                if(tiao){
                    location.href=res.data[0].power_list[0].url;
                }
            }
            if (res.resStatus === 1) {
                location.href = key_address;
            }
        },
        error: function () {
            location.href = key_address;
        }
    });
} else {
    location.href = key_address;
}
$(function () {
    var houtai = {
        linshi_search: {
            class1: "",
            class2: "",
            class3: "",
            search: "",
            supplier:"",
            types:"",
            warnAmount:null
        },
        linshi_record:{
            type:0,
            start_time:"",
            end_time:"",
            way:""
        },
        linshi_id: null, //临时的id存储 用来判断同一个按钮的操作id
        linshi_arrid: [],
        repertory_type: null, // 1入库 2出库 提交时根据类型判断请求
        list_ajax_page:1,
        //提示功能
        hint:function (str) {
            $(".hint").remove();
            $("body").append("<div class='hint'>"+str+"</div>");
            $(".hint").slideDown(300).delay(2000).slideUp();
        },
        //防止频繁操作
        old_click_time: 1000,
        click_yes: function () {
            var now_tiem = new Date().getTime();
            if (now_tiem - houtai.old_click_time > 1000) {
                houtai.old_click_time = now_tiem;
                return true;
            } else {
                return false;
            }
        },
        // 检查功能一般检查是否为空
        jiancha: function () {
            for (var i = 0; i < $(".add_goods_body .input_is:visible").length; i++) {
                if ($(".add_goods_body .input_is:visible").eq(i).val() === "") {
                    $(".add_goods_body .input_is:visible").eq(i).focus();
                    houtai.hint($(".add_goods_body .input_is:visible").eq(i).prev().text() + " 没有填写！");
                    return false;
                }
            }
            return true;
        },
        // 导出excel
        excel:(function () {
            var uri = 'data:application/vnd.ms-excel;base64,',
                template = '<html  xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta charset="UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
                base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))); },
                format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }); };
            return function (table, name) {
                if (!table.nodeType) table = document.getElementById("tableExcel");
                var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML };
                if($("#dlink").length<1)$("body").append($("<a id='dlink' style='display: none'></a>"));

                document.getElementById("dlink").href = uri + base64(format(template, ctx));
                document.getElementById("dlink").download = name+'.xls';
                document.getElementById("dlink").click();
            };
        })(),
        // 获取商品的数组
        shop_list_ajax: function (page_number) {
            var n = page_number===undefined ?houtai.list_ajax_page=1  : page_number;
            var ispring=n!==0;
            $.ajax({
                url: ip + 'index.php?s=admincms/Inventory/showlist',
                type: "post",
                async:ispring,
                data: {
                    pid: houtai.linshi_search.class1,
                    cid: houtai.linshi_search.class2,
                    catId: houtai.linshi_search.class3,
                    warnAmount:houtai.linshi_search.warnAmount,
                    supplier:houtai.linshi_search.supplier,
                    types:houtai.linshi_search.types,
                    page: n,
                    search: houtai.linshi_search.search,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    houtai.table_string(res);
                    $("#page").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.list_ajax_page=pageIndex;
                            houtai.shop_list_ajax(pageIndex);
                        }
                    });
                }

            });
        },
        // 把获取的商品数组写入table
        table_string: function (res) {
            console.log(res);
            // 写入table
            var string_head = "<thead> <tr> <th>编号</th> <th>名称</th> <th>数量</th> <th>低报警量 <span class='toggle_warnAmount glyphicon glyphicon-sort btn btn-default btn-xs'></span></th> <th>入库均价</th>  <th>入库历史</th> <th>出库历史</th> <th>操作</th> </tr> </thead>";
            var string_body = "";
           if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    var chuku = !o.outbound.amount ?  "无":'<div class="item_history"> <div title="点击查看所有历史"> <b><span>总数量:</span><b class="outbound_tatle">' + o.outbound.amount + '</b></b><span title="查看全部历史" class="btn btn-info btn-xs outbound_all">全部</span></div><p><span>数量：</span>' + o.outbound.oneamount + '</p> <p><span>备注：</span>' + o.outbound.descript + '</p><p>' + o.outbound.addtime + '</p>  </div>' ;
                    var ruku = !o.storage.amount  ? "无" :'<div class="item_history"> <div title="点击查看所有历史"> <b><span>总数量:</span><b class="inbound_tatle">' + o.storage.amount + '</b></b><span title="查看全部历史" class="btn btn-info btn-xs inbound_all">全部</span></div> <p><span>数量：</span>' + o.storage.oneamount + '</p><p><span>备注：</span>' + o.storage.descript + '</p><p>' + o.storage.addtime + '</p>  </div> ' ;
                    var wran_class=o.amount<o.warnAmount?"wraning_amount":"";
                    string_body += '<tr>' +
                        '<td class="repertory_number">' + o.id + '</td>' +
                        '<td class="repertory_name">' + o.names + '</td>' +
                        '<td class="repertory_n">' + o.amount + '</td>' +
                        '<td class="dibaojingliang '+wran_class+'">' + o.warnAmount + '</td>' +
                        '<td class="repertory_price">' + o.avgPrice + '</td>' +
                        '<td style="width: 25%;vertical-align: top;" class="inbound_history">' + ruku + '</td>' +
                        '<td style="width:25%;vertical-align: top;" class="outbound_history">' + chuku + '</td>' +
                        '<td style="width:100px;"> <b class="btn btn-info btn-xs inbound">入库</b> <b class="btn btn-primary btn-xs outbound">出库</b> </td> </tr>';
                });
            } else{
                string_body = "<tr class='text-center'><td colspan='6'>没有数据</td></tr>";
            }

                $("#repertory_table").empty().append(string_head + string_body);
        },
        //初始类
        chushi_class: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/product/product",
                type: "post",
                data: {
                    token:get_cookie("key")
                },
                async: false,
                success: function (_res) {
                    var res=JSON.parse(_res);
                    if (res.resStatus === 0) {
                        houtai.arr = res.data;
                        for (var ii = 0, string11 ='<option value="">全部</option>'; ii < houtai.arr.length; ii++) {
                            string11 += '<option value="' + houtai.arr[ii].pid + '">' + houtai.arr[ii].typesnames + '</option>';
                        }
                        $("#select1").empty().append(string11);
                        $("#select2").empty().append('<option value="">全部</option>');
                        $("#select3").empty().append('<option value="">全部</option>');
                    }
                },
                error:function () {
                   houtai.hint("初始类失败！！");
                }
            });
        },
        // 请求历史的出入库
        repertory_history: function (pages) {
            var n = pages ? pages : 1;
            if (houtai.repertory_type == 1) {
                $.ajax({
                    url: ip + "index.php?s=admincms/Inventory/detailsLog",
                    type: "post",
                    data: {
                        tp: 0,
                        page: n,
                        id: houtai.linshi_id,
                        token:get_cookie("key")
                    },
                    success: function (_res) {
                        var res=JSON.parse(_res);
                        houtai.inbound_history_string(res);
                        $("#page_inbound").sdpagination({
                            boxClass: 'sdpagination',
                            totalCount: res.totalpage,           //总条数
                            pageSize: 10,                //每页显示
                            pageIndex: n,                //当前页数
                            preText: '上页',                                  //上一页按钮显示的字符
                            nextText: '下页',                                 //下一页按钮显示的字符
                            firstText: '首页',                                //首页按钮显示的字符
                            lastText: '末页',                                 //末页按钮显示的字符
                            showNearby: 3,                                    //当前按钮前后按钮数
                            infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                            pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                            onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                                houtai.repertory_history(pageIndex);
                            }
                        });

                    },
                    error:function () {
                        houtai.hint("请求失败！！");
                    }
                });
            }
            if (houtai.repertory_type == 2) {
                $.ajax({
                    url: ip + "index.php?s=admincms/Inventory/detailsLog",
                    type: "post",
                    data: {
                        tp: 1,
                        page: n,
                        id: houtai.linshi_id,
                        token:get_cookie("key")
                    },
                    success: function (res) {
                        houtai.outbound_history_string(JSON.parse(res));
                        if (!pages) {
                            houtai.hint("<span style='color:#0f0;'>为你找到" + JSON.parse(res).totalpage + "条数据</span>");
                        }
                        $("#page_inbound").sdpagination({
                            boxClass: 'sdpagination',
                            totalCount: JSON.parse(res).totalpage,           //总条数
                            pageSize: 10,                //每页显示
                            pageIndex: n,                //当前页数
                            preText: '上页',                                  //上一页按钮显示的字符
                            nextText: '下页',                                 //下一页按钮显示的字符
                            firstText: '首页',                                //首页按钮显示的字符
                            lastText: '末页',                                 //末页按钮显示的字符
                            showNearby: 3,                                    //当前按钮前后按钮数
                            infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                            pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                            onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                                houtai.repertory_history(pageIndex);
                            }
                        });
                    },
                    error:function () {
                        houtai.hint("请求失败！！");
                    }

                });
            }
        },
        //入库历史详情table
        inbound_history_string: function (res) {
            console.log(res);
            var data = res.data;
            // 写入table
            var string_head = "<thead> <tr> <th>名称</th> <th>入库时间</th> <th>入库数量</th><th>进货价</th><th>供应商</th> <th>备注</th> <th>入库人</th> </tr> </thead>";
            var string_body = "";
             if (res.resStatus === 0) {
                $.each(data, function (i, o) {
                    string_body += '<tr>' +
                        '<td>' + o.names + '</td>' +
                        '<td>' + o.addtime + '</td>' +
                        '<td>' + o.amount + '</td>' +
                        '<td>' + o.buyingprice + '</td>' +
                        '<td>' + o.sitenames + '</td>' +
                        '<td style="width:30%;">' + o.descript + '</td>' +
                        '<td>' + o.admin + '</td>' +
                        '</tr>';
                });
            }else{
                string_body = "<tr class='text-center'><td colspan='4'>没有找到哦 亲！</td></tr>";
                houtai.hint("没有找到哦 亲!");
            }

            $("#table_history").empty().append(string_head + string_body);
            $("#repertory_table_history").fadeIn();
        },
        //出库历史详情table
        outbound_history_string: function (res) {
            var data = res.data;
            console.log(data);
            // 写入table
            var string_head = "<thead> <tr> <th>名称</th> <th>出库时间</th> <th>出库数量</th> <th>销售价格</th> <th>收货人</th><th>备注</th>  <th>出库人</th> </tr> </thead>";
            var string_body = "";
            if (res.resStatus === 1) {
                string_body = "<tr class='text-center'><td colspan='4'>没有找到哦 亲！</td></tr>";
                houtai.hint("没有找到哦 亲!");
            } else if (res.resStatus === 0) {

                $.each(data, function (i, o) {
                    string_body += '<tr>' +
                        '<td>' + o.names + '</td>' +
                        '<td>' + o.addtime + '</td>' +
                        '<td>' + o.amount + '</td>' +
                        '<td>' + o.buyingprice + '</td>' +
                        '<td>' + o.sitenames + '</td>' +
                        '<td style="width:30%;">' + o.descript + '</td>' +
                        '<td>' + o.admin + '</td>' +
                        '</tr>';
                });
            }
            $("#table_history").empty().append(string_head + string_body);
            $("#repertory_table_history").fadeIn();
        },
        //商品 入库 出库
        repertory_ajax: function () {
            //入库
            if (houtai.repertory_type == 1) {
                $.ajax({
                    url: ip + "index.php?s=admincms/Inventory/addSave",
                    type: "post",
                    data: {
                        tp: 1,
                        id: houtai.linshi_id,
                        amount: $("#inbound_n_n").val(),
                        buyingprice: $("#inbound_price").val(),
                        descript: $("#inbound_mark").val(),
                        supplier:$("#inbound_supplier").val(),
                        token: get_cookie("key")
                    },
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        console.log(res);
                        if (res.resStatus === 0) {
                            $("#repertory_inbound").fadeOut();
                            $(".input_is").val("");
                            houtai.hint("<p class='text-success'>入库成功了！</p>");
                             houtai.shop_list_ajax(houtai.list_ajax_page);
                        } else if (res.resStatus === 11) {
                            alert("您的账户被他人登录，请重新登录！！！");
                            location.href = "index.html";
                        }else{
                            houtai.hint(res.message);
                        }

                    }
                });
            }
            //出库
            if (houtai.repertory_type === 2) {
                $.ajax({
                    url: ip + "index.php?s=admincms/Inventory/addSave",
                    type: "post",
                    data: {
                        tp: 2,
                        id: houtai.linshi_id,
                        amount: $("#outbound_n_n").val(),
                        descript: $("#outbound_mark").val(),
                        employ : $("#outbound_consignee").val(),
                        token: get_cookie("key")
                    },
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        if (res.resStatus === 0) {
                            houtai.shop_list_ajax(houtai.list_ajax_page);
                            houtai.hint("<p class='text-success'>出库成功了！</p>");
                            $(".input_is").val("");
                            $("#repertory_outbound").fadeOut();
                        }else if (res.resStatus === 3) {
                            houtai.hint("出库数量大于现有数量，不能出库");
                        } else if (res.resStatus === 11) {
                            alert("您的账户被他人登录，请重新登录！！！");
                            location.href = "index.html";
                        } else {
                            houtai.hint("出库失败了");
                        }

                    }
                });
            }
        },
        // 获取全部记录的数组
         record_all_ajax: function (page_number) {
        var n = page_number===undefined ?1 :page_number ;
        var ispring=n!==0;
        $.ajax({
            url: ip + 'index.php?s=admincms/Inventory/getInventoryLog',
            type: "post",
            async:ispring,
            data: {
                tp:houtai.linshi_record.type ,
                way:houtai.linshi_record.way,
                start_time :houtai.linshi_record.start_time ,
                end_time:houtai.linshi_record.end_time ,
                page:n,
                token:get_cookie("key")
            },
            success: function (_res) {
                var res = JSON.parse(_res);
                houtai.record_all_string(res);
                $("#page1").sdpagination({
                    boxClass: 'sdpagination',
                    totalCount: res.totalpage,           //总条数
                    pageSize: 10,                //每页显示
                    pageIndex: n,                //当前页数
                    preText: '上页',                                  //上一页按钮显示的字符
                    nextText: '下页',                                 //下一页按钮显示的字符
                    firstText: '首页',                                //首页按钮显示的字符
                    lastText: '末页',                                 //末页按钮显示的字符
                    showNearby: 3,                                    //当前按钮前后按钮数
                    infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                    pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                    onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                        houtai.record_all_ajax(pageIndex);
                    }
                });
            }

        });
    },
        // 把获取的记录数组写入table
        record_all_string: function (res) {
            console.log(res);
           var jiaojieren= houtai.linshi_record.type==1?"收货人":"供应商";
            // 写入table
            var string_head = "<thead> <tr> <th>分类</th><th>名称</th> <th>数量</th><th>价格</th><th>单位</th><th>总价</th><th>备注</th> <th>"+jiaojieren+"</th> <th>类型</th> <th>自动/手动</th> <th>时间</th> <th>操作人</th> </tr> </thead>";
            var string_body = "";
            if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    var type_string=o.tp==="0"?"入库":"出库";
                    var type_string1=o.way===0?"自动":"手动";
                    string_body += '<tr>' +
                        '<td class="all_record_class_name">' + o.className + '</td>' +
                        '<td class="all_record_name">' + o.names + '</td>' +
                        '<td class="all_record_number">' + o.amount + '</td>' +
                        '<td class="all_record_price">' + o.buyingprice + '</td>'+
                        '<td class="all_record_unit">' + o.unit + '</td>'+
                        '<td class="all_record_totle_price">' +  Math.float_(o.amount*o.buyingprice,2) + '</td>'+
                        '<td style="min-width: 150px" class="all_record_remark">' + o.descript + '</td>'+
                        '<td class="jiaojieren">' + o.sitenames + '</td>'+
                        '<td class="all_record_type">' + type_string + '</td>'+
                        '<td class="all_record_type1">' + type_string1+ '</td>'+
                        '<td class="all_record_time">' + o.addtime + '</td>'+
                        '<td class="all_record_admin">' + o.admin + '</td>';
                });
            } else{
                string_body = "<tr class='text-center'><td colspan='6'>没有数据</td></tr>";
            }

            $("#add_taocan_table").empty().append(string_head + string_body);
        },
        //供应商全部数据请求
        gongyingshang_ajax:function () {
            $.ajax({
                url: ip + "index.php?s=admincms/supplier/show",
                type: "post",
                data:{
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res=JSON.parse(_res);
                    if(res.resStatus===0){
                        var set_string='<option value="">请选择</option>';
                        var seller_string='<option value="">全部</option>';
                        $.each(res.data,function (i,o) {
                            set_string+='<option value="'+o.id+'">'+o.sitenames+'</option>';
                            seller_string+='<option value="'+o.id+'">'+o.sitenames+'</option>';

                        });
                        $("#inbound_supplier").empty().append(set_string);
                        $("#seller").empty().append(seller_string);
                    }else{
                        houtai.hint("供应商请求失败！！");
                    }
                },
                error:function () {
                    houtai.hint("供应商请求失败！！");
                }

            });
        }
    };
    houtai.chushi_class();
    houtai.linshi_search.class1 = $("#select1").val();
    houtai.linshi_search.class2 = $("#select2").val();
    var locastring=location.search.match(/\d+/)&&location.search.match(/\d+/)[0]||"";
    for(var i=0,str="";i<locastring.length;i+=5){
        str+= String.fromCharCode(locastring.slice(i,i+5));
    }
    houtai.linshi_search.search=str;
    houtai.shop_list_ajax();
    houtai.gongyingshang_ajax();
// 收展事件
    $(".right-top>b").bind("click", function () {
        if ($(".content_left").is(":hidden")) {
            $(".content_left").removeClass("ishidden");
            $(".content_right").removeClass("iskuozhang");
        } else {
            $(".content_left").addClass("ishidden");
            $(".content_right").addClass("iskuozhang");
        }
    });

    // 一级下拉事件
    $("#select1").bind("change", function () {
        if($(this).val()===""){
            $("#select2").empty().append('<option value="">全部</option>');
            $("#select3").empty().append('<option value="">全部</option>');
        }else{
            for (var b = 0,string2="<option value=''>全部</option>",string3="<option value=''>全部</option>"; b < houtai.arr.length; b++) { //循环分类查找对应的子集
                if (houtai.arr[b].pid == $(this).val()) {
                    for (var a = 0; a < houtai.arr[b].activity_list.length; a++) {
                        string2 += '<option value="' + houtai.arr[b].activity_list[a].cid + '">' + houtai.arr[b].activity_list[a].classnames + '</option>';
                    }
                    break;
                }
            }
            $("#select2").empty().append(string2);
            $("#select3").empty().append('<option value="">全部</option>');
        }

        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 =$("#select2").val();
        houtai.linshi_search.class3 =$("#select3").val();
        houtai.linshi_search.search = "";
        houtai.linshi_search.supplier=$("#seller").val();
        houtai.linshi_search.types=$("#isAutotrophy").val();
        houtai.linshi_search.warnAmount = null;
        houtai.shop_list_ajax();
    });
    // 二级下拉事件
    $("#select2").bind("change", function () {
        if($(this).val()===""){
            $("#select3").empty().append('<option value="">全部</option>');
        }else {
            for (var b = 0, string3 = "<option value=''>全部</option>"; b < houtai.arr.length; b++) { //循环分类查找对应的子集
                if (houtai.arr[b].pid == $("#select1").val()) {
                    for (var a = 0; a < houtai.arr[b].activity_list.length; a++) {
                        if (houtai.arr[b].activity_list[a].cid == $(this).val()) {
                            for (var bb = 0; bb < houtai.arr[b].activity_list[a].cats_list.length; bb++) {
                                string3 += '<option value="' + houtai.arr[b].activity_list[a].cats_list[bb].catId + '">' + houtai.arr[b].activity_list[a].cats_list[bb].catName + '</option>';
                            }
                            $("#select3").empty().append(string3);
                            break;
                        }
                    }
                    break;
                }
            }
        }
        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 =$("#select2").val();
        houtai.linshi_search.class3 =$("#select3").val();
        houtai.linshi_search.search = "";
        houtai.linshi_search.supplier=$("#seller").val();
        houtai.linshi_search.types=$("#isAutotrophy").val();
        houtai.linshi_search.warnAmount = null;
        houtai.shop_list_ajax();
    });
    // 三级下拉事件
    $("#select3").bind("change", function () {
        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 =$("#select2").val();
        houtai.linshi_search.class3 =$("#select3").val();
        houtai.linshi_search.search = "";
        houtai.linshi_search.supplier=$("#seller").val();
        houtai.linshi_search.types=$("#isAutotrophy").val();
        houtai.linshi_search.warnAmount = null;
        houtai.shop_list_ajax();
    });

    //搜索库存商品
    $("#search_btn").bind("click", function () {
        if ($.trim($("#search").val()) === "") {
            houtai.hint("请输入后再查询");
            $("#search").focus();
        } else {
            houtai.linshi_search.class1 = "";
            houtai.linshi_search.class2 = "";
            houtai.linshi_search.class3 = "";
            houtai.linshi_search.search = $.trim($("#search").val());
            houtai.linshi_search.supplier="";
            houtai.linshi_search.types="";
            houtai.linshi_search.warnAmount = null;
            houtai.shop_list_ajax();
        }
    });
    //回车按钮操作
    $("#search_kucun_form").submit(function () {
        if ($.trim($("#search").val()) === "") {
            houtai.hint("请输入后再查询");
            $("#search").focus();
        } else {
            houtai.linshi_search.class1 = "";
            houtai.linshi_search.class2 = "";
            houtai.linshi_search.class3 = "";
            houtai.linshi_search.search = $.trim($("#search").val());
            houtai.linshi_search.supplier="";
            houtai.linshi_search.types="";
            houtai.linshi_search.warnAmount = null;
            houtai.shop_list_ajax();
        }
        return false;
    });
    //按供应商搜索
    $("#seller").bind("change", function () {
        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 = $("#select2").val();
        houtai.linshi_search.class3 = $("#select3").val();
        houtai.linshi_search.state =  $("#select4").val();
        houtai.linshi_search.supplier=$("#seller").val();
        houtai.linshi_search.types=$("#isAutotrophy").val();
        houtai.linshi_search.search = "";
        houtai.shop_list_ajax();
    });
    //自营所搜
    $("#isAutotrophy").bind("change", function () {
        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 = $("#select2").val();
        houtai.linshi_search.class3 = $("#select3").val();
        houtai.linshi_search.state =  $("#select4").val();
        houtai.linshi_search.supplier=$("#seller").val();
        houtai.linshi_search.types=$("#isAutotrophy").val();
        houtai.linshi_search.search = "";
        houtai.shop_list_ajax();
    });
    //修改事件
    $("#set_add").bind("click", function () {
        if (houtai.jiancha()) {
            houtai.xiugai();
        }
    });
    // 莫态框取消modal
    $("#modal_close").bind("click", function () {
        $("#modal_wrap").fadeOut(200);

    });
    // 修改取消
    $(".cancle_set").bind("click", function () {
        $(".set_shangpin").fadeOut(200);

    });
    //历史查询弹框关闭
    $("#close_table_history").bind("click", function () {
        $("#repertory_table_history").fadeOut(200);
    });
    //查看全部入库历史
    $("#repertory_table").on("click", ".inbound_history .inbound_all", function () {
        houtai.repertory_type = 1;
        houtai.linshi_id = $(this).parents("tr").find(".repertory_number").text();
        houtai.repertory_history();
    });
    //查看全部出库历史
    $("#repertory_table").on("click", ".outbound_history .outbound_all", function () {
        houtai.repertory_type = 2;
        houtai.linshi_id = $(this).parents("tr").find(".repertory_number").text();
        houtai.repertory_history();
    });
    //入库按钮
    $("#repertory_table").on("click", ".inbound", function () {
        houtai.linshi_id = $(this).parents("tr").find(".repertory_number").text();
        houtai.repertory_type = 1;
        $(".set_shangpin").hide();
        $("#repertory_inbound").fadeIn();
        $("#inbound_name").val($(this).parents("tr").find(".repertory_name").text());
        $("#inbound_number").val($(this).parents("tr").find(".repertory_number").text());
        $("#inbound_y_n").val($(this).parents("tr").find(".repertory_n").text());
    });
    //出库按钮
    $("#repertory_table").on("click", ".outbound", function () {
        houtai.linshi_id = $(this).parents("tr").find(".repertory_number").text();
        houtai.repertory_type = 2;
        $(".set_shangpin").hide();
        $("#repertory_outbound").fadeIn();
        $("#outbound_name").val($(this).parents("tr").find(".repertory_name").text());
        $("#outbound_number").val($(this).parents("tr").find(".repertory_number").text());
        $("#outbound_y_n").val($(this).parents("tr").find(".repertory_n").text());
    });
    $("#repertory_table").on("click", ".toggle_warnAmount", function () {
        if(houtai.linshi_search.warnAmount===null){
            houtai.linshi_search.warnAmount=false;
        }else if(houtai.linshi_search.warnAmount===true){
            houtai.linshi_search.warnAmount=false;
        }else{
            houtai.linshi_search.warnAmount=true;
        }
            houtai.shop_list_ajax();

    });
    //出入库确认按钮
    $(".add").bind("click", function () {
        if (houtai.click_yes()) {
            if (houtai.jiancha()) {
                houtai.repertory_ajax();
            }
        }

    });
    //导出excel
    $("#print_repertory").bind("click",function () {

        if($("#repertory_table tr:last>td").length>1){
            houtai.shop_list_ajax(0);
            var title="库存管理-"+$("#select1 option:selected").text()+"-"+$("#select2 option:selected").text();
            houtai.excel($("#repertory_table")[0],title);
        }else{
            houtai.hint("没有什么导出的！！");
        }

    });
    //记录查询按钮
    $("#record_btn").bind("click",function () {
     $("#repertory_record").fadeIn();
        houtai.record_all_ajax();
    });
    //记录类型选择下拉
    $("#bound_type").bind("change",function () {
        houtai.linshi_record.start_time = "";
        houtai.linshi_record.end_time= "";
        houtai.linshi_record.type=$(this).val();
       if($(this).val()==="1"){
           houtai.linshi_record.way=$("#outbound_type").val();
          $("#outbound_type_wrap").fadeIn();
       }else{
           houtai.linshi_record.way="";
           $("#outbound_type").val("");
           $("#outbound_type_wrap").fadeOut();
       }
       houtai.record_all_ajax();
    });
    //出库类型下拉
    $("#outbound_type").bind("change",function () {
        houtai.linshi_record.start_time = "";
        houtai.linshi_record.end_time= "";
        houtai.linshi_record.way=$("#outbound_type").val();
        houtai.record_all_ajax();
    });
    //时间插件
    $('#input_time').daterangepicker({
        timePicker: true,
        timePickerIncrement: 30,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });
    $("#input_time_btn").bind("click", function () {
        $("#input_time").click();
    });
    // 按时间搜索
    $("#search_time").bind("click", function () {
        if ($.trim($("#input_time").val()) === "") {
            $("#input_time").focus();
            houtai.hint("请输入搜索内容");
        } else {
            if (houtai.click_yes()) {
                console.log($("#input_time").val());
                // 匹配的正则表达式
                var regexp = /(\d+-\d+-[^-]+)-(.+)/;
                houtai.linshi_record.type=$("#bound_type").val();
                houtai.linshi_record.way=$("#outbound_type").val();
                houtai.linshi_record.start_time = new Date($("#input_time").val().match(regexp)[1]).getTime() / 1000;
                houtai.linshi_record.end_time= new Date($("#input_time").val().match(regexp)[2]).getTime() / 1000;
                houtai.record_all_ajax();
            }
        }

    });
    //取消记录框按钮
    $("#close_record").bind("click", function () {
        $("#repertory_record").fadeOut();
    });
    //所有记录打印按钮
     $("#record_all_btn").bind("click",function () {
         if($("#add_taocan_table tr:last>td").length>1){
             houtai.record_all_ajax(0);
             var title=houtai.linshi_record.start_time?("库存记录-"+$("#bound_type option:selected").text()+"-"+$("#input_time").val()):("库存记录-"+$("#bound_type option:selected").text());
             houtai.excel($("#add_taocan_table")[0],title);
         }else{
             houtai.hint("没有什么导出的！！");
         }
     });
    //退出按钮
    $("#exit").bind("click", function () {
        set_cookie("key", "", 0);
        location.href = key_address;
    });
    //头像按钮
    $(".right-top>div").bind("click", function (e) {
        $("#exit").slideToggle();
        e.stopPropagation();
    });
    $(window).bind("click", function () {
        $("#exit").slideUp();
    });
});